package com.li.onjava8.concurrent.cf;

import com.li.onjava8.concurrent.es.task.QuittableTask;
import com.li.onjava8.concurrent.utils.Nap;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 使用CompletableFutures在
 * @author li
 * @date 2023/10/30 15:14
 */
public class QuittingCompletable {
    public static void main(String[] args) {
        List<QuittableTask> tasks = IntStream.range(0, 150)
                .mapToObj(QuittableTask::new)
                .collect(Collectors.toList());
        //runAsync: 返回一个新的CompletableFuture，运行给定的动作后异步完成。
        List<CompletableFuture<Void>> futures = tasks.stream()
                .map(CompletableFuture::runAsync)
                .collect(Collectors.toList());
        new Nap(1);
        tasks.forEach(QuittableTask::quit);
        futures.forEach(CompletableFuture::join);
    }

}
